// 已经设置的上车提醒站点列表和已经设置了下车提醒的车辆列表
let attentionStationList, attentionBusList

// // 设置了到站提醒的站点列表 STATION_SORT 站点序号
// let attentionStationList = [
//   { LINE_ID: '1011', LINE_TYPE: 1, STATION_ID: '22', STATION_SORT: 4 }
// ]
// // 设置了下车提醒的车辆列表
// let attentionBusList = [{ LINE_ID: '1011', LINE_TYPE: 1, BUS_ID: '浙G26897' }]

/** 以下函数为内部调用，不对外开放 */

/**
 * 插件调用前打印，用于查看调用的插件名知否准确及入参是否准确
 *
 * @param {*} pluginName
 * @param {*} param
 */
function logBeforeDispath(pluginName, param = '无') {
  console.log(
    '&&&&&&&&&&&&=>>>>>' +
      pluginName +
      '插件准备调用,入参:' +
      param +
      '<<<<<=&&&&&&&&&&&&'
  )
}

/**
 * 插件成功回调打印，用于查看插件是否成功回调及回调的参数
 *
 * @param {*} pluginName
 * @param {*} param
 */
function logSuccessCallBack(pluginName, param = '无') {
  console.log(
    '&&&&&&&&&&&&=>>>>>' +
      pluginName +
      '插件成功回调,参数:' +
      param +
      '<<<<<=&&&&&&&&&&&&'
  )
}

/**
 * 插件回调失败打印
 *
 * @param {*} pluginName
 */
function logFailureCallBack(pluginName) {
  console.log(
    '&&&&&&&&&&&&=>>>>>' + pluginName + '插件调用失败!!!' + '<<<<<=&&&&&&&&&&&&'
  )
}
// let index = 0
/**
 * 获取设置了到站提醒的站点列表
 */
async function getAttentionStationList(selfVueModule) {
  logBeforeDispath('getAttentionStationList')
  // if (!window.prodInApp) {
  //   let res =
  //     '[{ "LINE_ID": "1011", "LINE_TYPE": "1", "STATION_ID": "47", "STATION_SORT": "1" }]'
  //   logSuccessCallBack('getAttentionStationList', res)
  //   attentionStationList = index % 2 === 0 ? JSON.parse(res) : []
  //   index++
  //   console.log('~~~getAttentionStationList' + index, attentionStationList)
  //   return Promise.resolve()
  // }
  return new Promise((resolve, reject) => {
    selfVueModule.$ThirdParty
      .then(thirdParty => {
        thirdParty.getAttentionStationList(
          res => {
            logSuccessCallBack('getAttentionStationList', res)
            attentionStationList = JSON.parse(res)
            resolve()
          },
          () => {
            logFailureCallBack('getAttentionStationList')
            attentionStationList = []
            resolve()
          }
        )
      })
      .catch(() => {
        reject(new Error('no plugin getAttentionStationList'))
      })
  })
}

/**
 * 获取设置了下车提醒的车辆列表
 */
async function getAttentionBusList(selfVueModule) {
  logBeforeDispath('getAttentionBusList')
  return new Promise((resolve, reject) => {
    selfVueModule.$ThirdParty
      .then(thirdParty => {
        thirdParty.getAttentionBusList(
          res => {
            logSuccessCallBack('getAttentionBusList', res)
            attentionBusList = JSON.parse(res)
            resolve()
          },
          () => {
            logFailureCallBack('getAttentionBusList')
            attentionBusList = []
            resolve()
          }
        )
      })
      .catch(() => {
        reject(new Error('no plugin getAttentionBusList'))
      })
  })
}

/**
 * 判断是否设置了到站提醒，如果设置了则弹框提示并取消，否则直接resolve
 * @param {*} vueModule
 */
// async function hasAttentionStation(vueModule) {
//   return new Promise((resolve, reject) => {
//     if (attentionStationList && attentionStationList.length > 0) {
//       logBeforeDispath('ui.showDialog')
//       vueModule.$UI.then(ui => {
//         ui.showDialog(
//           '提示',
//           '您已设置了上车提醒，若重新设置之前的提醒将失效！',
//           '继续',
//           '取消',
//           index => {
//             logSuccessCallBack('ui.showDialog', index)
//             if (!index || index === 0) {
//               // 确定，取消之前设置的到站提醒
//               let station = attentionStationList[0]
//               setAttentionStation(vueModule, station, false).then(() => {
//                 resolve()
//               })
//             } else {
//               // 取消操作
//               reject(new Error('cancel'))
//             }
//           },
//           () => {
//             logFailureCallBack('ui.showDialog')
//           }
//         )
//       })
//     } else {
//       resolve()
//     }
//   })
// }

/** 以下函数为外部调用，不对外开放 */

/**
 * 判断当前站点是否设置了到站提醒
 * @param {Object} station
 */
function stationIsAttention(station) {
  if (attentionStationList && attentionStationList.length > 0) {
    let item = attentionStationList.find(
      item =>
        item.LINE_ID + '' === station.routeId + '' &&
        item.LINE_TYPE + '' === station.direction + '' &&
        item.STATION_ID + '' === station.stationId + '' &&
        item.STATION_SORT + '' === station.sort + ''
    )
    if (item) return true
    else return false
  } else {
    return false
  }
}

/**
 * 判断当前车子是否设置了下车提醒
 * @param {Object} bus
 */
function busIsAttention(bus) {
  if (attentionBusList && attentionBusList.length > 0) {
    let item = attentionBusList.find(
      item =>
        item.LINE_ID + '' === bus.routeId + '' &&
        item.LINE_TYPE + '' === bus.direction + '' &&
        item.BUS_ID + '' === bus.busCard + ''
    )
    if (item) return true
    else return false
  } else {
    return false
  }
}

/**
 * 清空所有的数据.并且重新去底层获取设置了上车提醒的车站列表和设置了下车 提醒的车辆列表
 */
async function resetAllAttentionData(vueModule) {
  // return new Promise((resolve, reject) => {
  //   Promise.all([
  //     (getAttentionBusList(vueModule), getAttentionStationList(vueModule))
  //   ])
  //     .then(() => {
  //       resolve()
  //     })
  //     .catch(() => {
  //       resolve()
  //     })
  // })

  return Promise.all([
    getAttentionBusList(vueModule),
    getAttentionStationList(vueModule)
  ])
}

/**
 * 设置到站提醒，设置和取消设置都调用该插件
 * @param {Object} station
 * @param {Boolean} attention
 */
async function setAttentionStation(selfVueModule, station, attention) {
  logBeforeDispath(
    'setAttentionStation',
    'lineID=>' +
      station.routeId +
      ',lineType=>' +
      station.direction +
      ',stationID=>' +
      station.stationId +
      ',sort=>' +
      station.sort +
      ',attention=>' +
      attention
  )
  return new Promise((resolve, reject) => {
    // if (attention) {
    // hasAttentionStation(selfVueModule)
    //   .then(() => {
    // selfVueModule.$ThirdParty.then(thirdParty => {
    //   thirdParty.setAttentionStation(
    //     () => {
    //       logSuccessCallBack('setAttentionStation')
    //       resolve()
    //     },
    //     () => {
    //       logFailureCallBack('setAttentionStation')
    //       reject(new Error('err'))
    //     },
    //     station.routeId + '',
    //     station.direction + '',
    //     station.stationId + '',
    //     station.sort + '',
    //     attention
    //   )
    // })
    // })
    // .catch(() => {
    //   // 取消,不设置到站提醒
    //   resolve()
    // })
    // } else {
    selfVueModule.$ThirdParty.then(thirdParty => {
      thirdParty.setAttentionStation(
        () => {
          logSuccessCallBack('setAttentionStation')
          resolve()
        },
        () => {
          logFailureCallBack('setAttentionStation')
          reject(new Error('err'))
        },
        station.routeId,
        station.direction,
        station.stationId,
        station.sort,
        attention
      )
    })
    // }
  })
}

/**
 * 车辆信息模态弹窗
 * @param {Object} bus
 */
async function showBusInfoMask(selfVueModule, bus) {
  logBeforeDispath(
    'showBusInfoMask',
    'lineID=>' +
      bus.routeId +
      ',lineType=>' +
      bus.direction +
      ',busID=>' +
      bus.busCard
  )
  return new Promise((resolve, reject) => {
    selfVueModule.$ThirdParty.then(thirdParty => {
      thirdParty.showBusInfoMask(
        () => {
          logSuccessCallBack('showBusInfoMask')
          resolve()
        },
        () => {
          logFailureCallBack('showBusInfoMask')
          reject(new Error('err'))
        },
        bus.routeId + '',
        bus.direction + '',
        bus.busCard + ''
      )
    })
  })
}

export {
  stationIsAttention,
  busIsAttention,
  resetAllAttentionData,
  setAttentionStation,
  showBusInfoMask
}
